<?php 
class ControllernewsCategoryNews extends Controller {  
	public function index() {
		$this->load->model('tool/seo_url');  
		
		$this->language->load('news/category_news');
	
		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
      		'href'      => $this->model_tool_seo_url->rewrite($this->url->http('common/home')),
       		'text'      => $this->language->get('text_home'),
       		'separator' => FALSE
   		);	
   		$this->document->breadcrumbs[] = array(
      		'href'      => $this->model_tool_seo_url->rewrite($this->url->http('news/tintuc')),
       		'text'      => $this->language->get('text_news'),
       		'separator' => $this->language->get('text_separator')
   		);
		$this->load->model('catalog/category_news');
		
		if (isset($this->request->get['path_news'])) {
			$category_news_info = $this->model_catalog_category_news->getcategory($this->request->get['path_news']);
			if($category_news_info) {
			$path_news = $this->request->get['path_news'];
			while ($category_news_info['parent_news_id'] != 0) {
				$path_news = $category_news_info['parent_news_id'] . '_' . $path_news;
				$category_news_info = $this->model_catalog_category_news->getcategory($category_news_info['parent_news_id']);
			}
			$parts = explode('_', $path_news);
			foreach ($parts as $path_news_id) {
				$category_news_info = $this->model_catalog_category_news->getcategory($path_news_id);
				
				$this->document->breadcrumbs[] = array(
					'href'      => $this->model_tool_seo_url->rewrite($this->url->http('product/category_news&path_news=' . $path_news_id)),
					'text'      => $category_news_info['name'],
					'separator' => $this->language->get('text_separator')
				);
			}
			}
			$category_news_id = $this->request->get['path_news'];
		} else {
			$category_news_id = 0;
		}

		$category_info = $this->model_catalog_category_news->getCategory($category_news_id);
	
		if ($category_info) {
			$url_seo = $this->model_tool_seo_url->rewrite($this->url->http('news/category_news&path_news=' . $category_news_id));
			if($this->config->get('config_seo_url')) {
			if(isset($this->request->get['_route_'])){
			if ($url_seo != (HTTP_SERVER . $this->request->get['_route_'])){
			$this->redirect($url_seo);
			}
			} else {
				$this->redirect($url_seo);
			}
			}
	  		$this->document->title = $category_info['name'];
			
			$this->document->description = $category_info['meta_description'];
			
			$this->data['heading_title'] = $category_info['name'];
			
			$this->data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
			
			$this->data['text_sort'] = $this->language->get('text_sort');

			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
				
				$this->document->breadcrumbs[] = array(
					'href'      => $this->model_tool_seo_url->rewrite($this->url->http('news/category_news&path_news=' . $path_news))  . '?page=' . $page,
					'text'      => $category_info['name'] . ' - ' . sprintf($this->language->get('text_page'),$page),
					'separator' => $this->language->get('text_separator')
				);
				
				$this->document->title = $category_info['name'] . ' - ' . sprintf($this->language->get('text_page'),$page);
				
				$this->document->description = sprintf($this->language->get('text_page'),$page) . ' - ' . $category_info['meta_description'];
				
			} else { 
				$page = 1;
			}

			$this->load->model('catalog/category_news');  
			$this->load->model('catalog/news'); 
			 
			$category_total = $this->model_catalog_category_news->getTotalCategoriesByCategoryId($category_news_id);
			$news_total = $this->model_catalog_news->getTotalnewsByCategoryId($category_news_id);
			
			if ($category_total || $news_total ) {
				$results = $this->model_catalog_news->getnewssbyCatalog($category_news_id, ($page - 1) * 20, 20);

				$this->data['newss'] = array();

				foreach ($results as $result) {
					$first_img = '';
					$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'), $matches);
					if($matches [1]){
					$first_img = $matches [1] [0];
					} else {
					$first_img = "image/no_image.jpg";
					}
					
					$this->data['newss'][] = array(
						'item_id' => $result['news_id'],
						'title' => $result['title'],
						'date_added' => date('h:iA d/m/Y',strtotime($result['date_added'])),
						'description' => $result['description'],
						'image'     => $first_img,
						'href'  => $this->model_tool_seo_url->rewrite($this->url->http('news/news&news_id=' . $result['news_id']))
					);
				}
			
				$pagination = new Pagination();
				$pagination->total = $news_total;
				$pagination->page = $page;
				$pagination->limit = 20; 
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = $this->model_tool_seo_url->rewrite($this->url->http('news/category_news&path_news=' . $this->request->get['path_news'] . '&page=%s'));
			
				$this->data['pagination'] = $pagination->render();				

			
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/category_news.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/news/category_news.tpl';
				} else {
					$this->template = 'default/template/news/category_news.tpl';
				}	
				
				$this->children = array(
					'common/header',
					'common/footer',
					'common/column_left',
					'common/column_right'
				);
		
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));			
				
      		} else {
        		$this->document->title = $category_info['name'];
				
				$this->document->description = $category_info['meta_description'];
				
        		$this->data['heading_title'] = $category_info['name'];

        		$this->data['text_error'] = $this->language->get('text_empty');

        		$this->data['button_continue'] = $this->language->get('button_continue');

        		$this->data['continue'] = $this->url->http('common/home');
		
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
				} else {
					$this->template = 'default/template/error/not_found.tpl';
				}	
				
				$this->children = array(
					'common/header',
					'common/footer',
					'common/column_left',
					'common/column_right'
				);
		
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));					
      		}
    	} else {
			$url = '';

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			if (isset($this->request->get['path_news'])) {
	       		$this->document->breadcrumbs[] = array(
   	    			'href'      => $this->model_tool_seo_url->rewrite($this->url->http('news/category_news&path_news=' . $this->request->get['path_news'] . $url)),
    	   			'text'      => $this->language->get('text_error'),
        			'separator' => $this->language->get('text_separator')
        		);
			}
				
			$this->document->title = $this->language->get('text_error');

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = $this->url->http('common/home');
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}	
			
			$this->children = array(
				'common/header',
				'common/footer',
				'common/column_left',
				'common/column_right'
			);
		
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		}
  	}
}
?>